package com.example.jd.common;

import com.github.pagehelper.Page;

import java.util.List;

/**
 * @author fudaolai
 * @Data 2019/09/09
 * @param <T>
 */
public class PageInfoUtil<T> {

    public static final String PAGE_STRING = "1";
    public static final String SIZE_STRING = "10";
    public static final int PAGE = Integer.valueOf(PAGE_STRING);
    public static final int SIZE = Integer.valueOf(SIZE_STRING);

    private List<T> pagedItems;
    private int pageIndex;
    private int pageSize;
    private long totalCount;
    private long pages;

    public List<T> getPagedItems() {
        return pagedItems;
    }

    public void setPagedItems(List<T> pagedItems) {
        this.pagedItems = pagedItems;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public long getPages() {
        return pages;
    }

    public void setPages(long pages) {
        this.pages = pages;
    }

    public PageInfoUtil() {
        this(null, PageInfoUtil.PAGE, PageInfoUtil.SIZE, 0);
    }

    public PageInfoUtil(List<T> list) {
        this(list, PageInfoUtil.PAGE, list.size(), list.size());
    }

    public PageInfoUtil(List<T> list, int current, int pageSize, long total) {
        if (list instanceof Page) {
            Page<T> tPage = (Page) list;
            this.pageIndex = tPage.getPageNum();
            this.pageSize = tPage.getPageSize();
            this.totalCount = tPage.getTotal();
            this.pages = tPage.getPages();
            this.pagedItems = tPage;
        } else {
            this.pageIndex = current > 0 ? current : PageInfoUtil.PAGE;
            this.pageSize = pageSize > 0 ? pageSize : PageInfoUtil.SIZE;
            this.totalCount = total;
            // total == 0 &&
            if (pageSize == 0) {
                this.pages = 0;
            } else {
                this.pages = total / pageSize + (total % pageSize == 0 ? 0 : 1);
            }
            this.pagedItems = list;
        }
    }

    public PageInfoUtil(Page page){
        this.pageIndex = page.getPageNum();
        this.pageSize = page.getPageSize();
        this.totalCount = page.getTotal();
        this.pages = totalCount / pageSize + (totalCount % pageSize == 0 ? 0 : 1);
        this.pagedItems = page.getResult();
    }

}
